﻿using Dotnet.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sui.WinForm
{
    internal static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.ApplicationExit += Application_ApplicationExit;
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            Application.ThreadException += Application_ThreadException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            FrmLogin frmLogin = new FrmLogin(); 
            if(frmLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new FrmZfb());
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = e.ExceptionObject as Exception;
            LogFactory.GetLogger("Program.UnhandledException").Error($"异常消息:{ex.Message}\r\nStackTrace:{ex.StackTrace}");
            //FrmOutput.OutputLog.Post(new ActionBlockPostEvengArgs() { LogType = LogType.Error, Info = $"异常消息:{ex.Message}\r\nStackTrace:{ex.StackTrace}" });
        }

        private static void Application_ApplicationExit(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            Exception ex = e.Exception as Exception;
            LogFactory.GetLogger("Program.ThreadException").Error($"异常消息:{ex.Message}\r\nStackTrace:{ex.StackTrace}");
            
            //FrmOutput.OutputLog.Post(new ActionBlockPostEvengArgs() { LogType = LogType.Error, Info = $"异常消息:{ex.Message}\r\nStackTrace:{ex.StackTrace}" });
        }
    }
}
